package com.example.huadi1.service.Imp;

import com.example.huadi1.dto.StuGetBook;
import com.example.huadi1.dto.Textbook;
import com.example.huadi1.mapper.*;
import com.example.huadi1.service.StudentGetBookService;
import com.example.huadi1.service.TextbookService;
import com.example.huadi1.utils.AjaxResult;
import com.example.huadi1.utils.PageUtils;
import com.example.huadi1.utils.Request.GetAllBook;
import com.example.huadi1.utils.Request.GetBookRequest;
import com.example.huadi1.utils.Request.QueryBookRequest;
import com.example.huadi1.utils.Response.ClassGetBook;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
@Service
public class StudentGetBookServiceImp implements StudentGetBookService {
    @Autowired
    CourseSelectionMapper courseSelectionMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    TextbookMapper textbookMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    StuGetBookMapper stuGetBookMapper;
    @Autowired
    TextbookService textbookService;
    public AjaxResult GenerateStuGetBook(Integer stuId){
        Set<Integer> res=new HashSet<>();
        Integer stuClass=userMapper.findUserClassById(stuId);
        List<Integer> courseIds=courseSelectionMapper.getCourseIdByStuId(stuId);
        for(Integer courseId:courseIds){
            List<String> courseNames=courseMapper.getCourseNameByCourseId(courseId);
            for(String courseName:courseNames){
                List<Integer> textbookIdsByCourseName = textbookMapper.findTextbookIdsByCourseName(courseName);
                for(Integer textbookId:textbookIdsByCourseName){
                    res.add(textbookId);
                }
            }
        }
        List<Integer> textbookIds= new ArrayList<>(res);
        for(Integer textbookId:textbookIds){
            stuGetBookMapper.insertStuGetBook(new StuGetBook(textbookId,stuId,stuClass,0,0));
        }
        return AjaxResult.success();
    }
    public AjaxResult GenerateAllStuGetBook(){
        List<Integer> stuIds= courseSelectionMapper.getAllStuId();
        for(Integer stuId:stuIds){
            GenerateStuGetBook(stuId);
        }
        return AjaxResult.success();
    }
    public AjaxResult getStuBook(GetBookRequest getBookRequest){
        StuGetBook stuGetBook=stuGetBookMapper.selectStuGetBook(getBookRequest.getTextbookId(),getBookRequest.getUserId());
        stuGetBook.setIsStuGet(1);
        stuGetBookMapper.updateStuGetBook(stuGetBook);
//        Textbook textbook=textbookMapper.findById(stuGetBook.getTextbookId());
//        textbook.setStock(textbook.getStock()-1);
//        textbookService.updateTextbook(textbook);
        return AjaxResult.success();
    }

    public AjaxResult  getClassBook(GetBookRequest getBookRequest){
        List<StuGetBook> stuGetBooks = stuGetBookMapper.selectAllByClassNumAndTextbookId(getBookRequest.getUserId(),getBookRequest.getTextbookId());
        for(StuGetBook stuGetBook:stuGetBooks){
            stuGetBook.setIsClassGet(1);
            stuGetBookMapper.updateStuGetBook(stuGetBook);
        }
        Textbook textbook=textbookMapper.findById(getBookRequest.getTextbookId());
        textbook.setStock(textbook.getStock()-stuGetBooks.size());
        textbookService.updateTextbook(textbook);
        return AjaxResult.success();
    }
    public AjaxResult queryStuBook(QueryBookRequest queryBookRequest){
        PageHelper.startPage(queryBookRequest.getPageNum(), queryBookRequest.getPageSize());
        List<StuGetBook> stuGetBooks = stuGetBookMapper.selectStuGetBookByUserId(queryBookRequest.getUserId());
//        List<ClassGetBook> getBooks=new ArrayList<>();
//        for(StuGetBook stuGetBook:stuGetBooks) {
//            Integer canGet = 1;
//            if (stuGetBook.getIsStuGet() == 1 || stuGetBook.getIsClassGet() == 0) canGet = 0;
//            String courseName = textbookMapper.findById(stuGetBook.getTextbookId()).getCourseName();
//            getBooks.add(new ClassGetBook(stuGetBook.getStuId(), queryBookRequest.getRole(), courseName, stuGetBook.getTextbookId(), canGet));
//        }
        PageInfo<StuGetBook> pageAll = PageInfo.of(stuGetBooks);
        PageUtils pageUtils = new PageUtils(pageAll);
        AjaxResult result=AjaxResult.success(pageUtils);
        return result;
    }
    public AjaxResult getAllBook(GetAllBook getAllBook){
        List<StuGetBook> stuGetBooks = stuGetBookMapper.selectStuGetBookByUserIdAndClassGet(getAllBook.getUserId());
        for(StuGetBook stuGetBook:stuGetBooks){
            stuGetBook.setIsStuGet(1);
            stuGetBookMapper.updateStuGetBook(stuGetBook);
//            Textbook textbook=textbookMapper.findById(stuGetBook.getTextbookId());
//            textbook.setStock(textbook.getStock()-1);
//            textbookService.updateTextbook(textbook);
        }
        return AjaxResult.success();
    }
    public AjaxResult getAllClassBook(GetAllBook getAllBook){
        List<Integer> textbookIds=stuGetBookMapper.selectTextbookIdByClassNum(getAllBook.getUserId());
        for(Integer textbookId:textbookIds){
            GetBookRequest aClass = new GetBookRequest(getAllBook.getUserId(), "class", textbookId, 1);
            getClassBook(aClass);
        }
        return AjaxResult.success();
    }

    public AjaxResult queryClassBook(QueryBookRequest queryBookRequest){
        PageHelper.startPage(queryBookRequest.getPageNum(), queryBookRequest.getPageSize());
        List<StuGetBook> stuGetBooks = stuGetBookMapper.selectAllByClassNum(queryBookRequest.getUserId());
        List<ClassGetBook> classGetBooks=new ArrayList<>();
        for(StuGetBook stuGetBook:stuGetBooks){
            Integer canGet=1-stuGetBook.getIsClassGet();
            String courseName=textbookMapper.findById(stuGetBook.getTextbookId()).getCourseName();
            classGetBooks.add(new ClassGetBook(stuGetBook.getStuId(),queryBookRequest.getRole(),courseName,stuGetBook.getTextbookId(),canGet));
        }
        PageInfo<ClassGetBook> pageAll = PageInfo.of(classGetBooks);
        PageUtils pageUtils = new PageUtils(pageAll);
        AjaxResult result=AjaxResult.success(pageUtils);
        return result;
    }
}
